package collection;
import java.util.*;
public class CollectionDemo02 {
    public static void main(String[] args) {
        Collection<String> c = new ArrayList<>();
        c.add("one");
        c.add("two");
        c.add("three");
        c.add("four");
        c.add("five");
        System.out.println(c);
        System.out.println(c.size());
        System.out.println(c.isEmpty());
        /*
        c.clear();
        System.out.println("清空集合");
        System.out.println(c);//此时集合为空[]
         */
        //contains()判断当前集合中是否存在Monday
        boolean empty1 = c.contains("one");
        System.out.println(empty1);//输出T
        //remove():
        boolean empty2 = c.remove("five");
        System.out.println(empty2);//删除了five，返回T
        System.out.println("删除five后的集合为:"+c);//[one, two, three, four]
        //addALL()
        Collection<String> cc = new ArrayList<>();
        cc.add("Monday");
        cc.add("weekend");
        System.out.println("新的cc集合为:"+cc);//[Monday,weekend]
        c.addAll(cc);
        System.out.println("将cc新集合加入c集合后,c集合为:"+c);//[one, two, three, four, Monday, weekend]
        //containsAll()  判断c集合中是否包含cc集合中的元素
        boolean emptyAll = c.containsAll(cc);
        System.out.println(emptyAll);//T
        //retainAll()  取cc集合与c集合的交集
        //c.retainAll(cc);
        //System.out.println("c中仅包含两个集合的并集，此时的c集合为:"+c);//[Monday, weekend]
        // removeAll()
        cc.add("Monday");
        cc.add("weekend");
        System.out.println(cc);
        c.removeAll(cc);
        System.out.println("删除两个集合中的交集后c变为:"+c);//[one, two, three, four]

        /**
         * 迭代器
         * 加强for
         */
        Collection<String> c1 = new ArrayList<>();
        c1.add("one");
        c1.add("two");
        c1.add("three");
        c1.add("four");
        c1.add("five");
        Iterator<String> it = c1.iterator();
        while (it.hasNext()){
            String str =(String) it.next();
            System.out.println(str);
            if ("#".equals(str)){
                //c.remove(str);
                it.remove();
            }
        }
        for (Object obj : c1){
            String str = (String) obj;
            System.out.println(str);
        }

        //集合与数组的互转:
        //集合转化为数组
        String[] array = c1.toArray(new String[5]);
        System.out.println("转化后的数组为:"+Arrays.toString(array));
        //数组转化为集合
        String[] array1 = {"one","two","three","four","five"};
        System.out.println("array1:"+ Arrays.toString(array));
        List<String> list = Arrays.asList(array);
        System.out.println("转化后的集合为:"+list);
    }
}
